<template>
  <el-form v-loading="loading" :model="loginData" :rules="rules" ref="loginForm" label-position="left" label-width="0px" class="demo-ruleForm login-container">
    <h2 class="title">汽修宝业务管理平台</h2>
    <el-form-item prop="jobNum">
      <el-input autofocus type="text" v-model="loginData.jobNum" placeholder="员工号"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" v-model="loginData.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item style="width:100%;">
      <el-button type="primary" style="width:100%;" @click.native.prevent="doLogin">登录</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      loading:false,
      loginData:{
        jobNum:"",
        password:""
      },
      rules:{
        jobNum:[
          {required: true, message: '员工号不能为空', trigger: 'blur' }
        ],
        password:[
          {required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    doLogin(){
      if(this.loading)return;
      this.loading=true;
      this.$refs.loginForm.validate(
        valid=> {
            if(valid)this.doLoginExecute();
            else this.loading=false;
        }
      );
    },
    doLoginExecute(){
      var params=new URLSearchParams();
      params.append('jobNum',this.loginData.jobNum);
      params.append('password',this.loginData.password);
      this.$http.post(this.$store.state.url+"/admin/login",params).then(
        responese=>{
          this.loading=false;
          if(responese.data.status==200){
            window.sessionStorage.setItem("menuList",JSON.stringify(responese.data.data.menuList));
            window.sessionStorage.setItem("admin",JSON.stringify(responese.data.data.admin));
            window.onkeypress=event=>{}
            this.$router.push("/index");
          }else{
            this.$message.error(responese.data.message);
          }
        }
      ).catch();
    }
  },
  created:function(){
    window.onkeypress=event=>{
      if(event.keyCode==13)this.doLogin();
    }
  }
};
</script>

<style lang="scss" scoped>
.flex-space {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
.login-container {
  /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .remember {
    margin: 0px 0px 35px 0px;
  }
}
</style>